package edu.gatech.app4args.utils;

import java.util.ArrayList;

/**
 * A custom data structure to hold all the fields for a node read in from a
 * Compendium xml file. Several fields are unused at this point.
 *
 * @author Andrew Lunsford
 */

public class CompendiumNode {	
	/** A large int that identifies the node, used as the node's key in the node HashMap */
	private String id;
	/** An int that specifies the node's type */
	private int type;
	/** The node author */
	private String author;
	/** The nodes label, currently used as the only visible content */
	private String label;
	/** The longer content the node can store, unused as of current version */
	private String content;
	/** A list of parent node ids */
	private ArrayList<String> parents;
	/** A list of child node ids */
	private ArrayList<String> children;
	/** Used for comparison between nodes */
	private int hashCode;

	/**
	 * Creates a new node with the given values.
	 *
	 * @param id the node's id
	 * @param type the type of node
	 * @param author the node's author
	 * @param label the label of the node
	 * @param content the node's content
	 */
	public CompendiumNode(String id, int type, String author, String label, String content){
		this.id = id;
		this.type = type;
		this.author = author;
		this.label = label;
		this.content = content;
		this.parents = new ArrayList<String>();
		this.children = new ArrayList<String>();
	}

	/**
	 * Creates a node with blank values.
	 */
	public CompendiumNode(){
		this("", 0, "", "", "");
	}
	
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public int getType() {
		return type;
	}
	
	public void setType(int type) {
		this.type = type;
	}
	
	public String getAuthor() {
		return author;
	}
	
	public void setAuthor(String author) {
		this.author = author;
	}
	
	public String getLabel() {
		return label;
	}
	
	public void setLabel(String label) {
		this.label = label;
	}
	
	public String getContent(){
		return content;
	}
	
	public void setContent(String content){
		this.content = content;
	}
	
	public ArrayList<String> getChildren() {
		return children;
	}
	
	public void setChildren(ArrayList<String> children) {
		this.children = children;
	}

	/**
	 * Used to check if a CompendiumNode has children nodes.
	 * @return true if there are children, false if not
	 */
	public boolean hasChildren(){
		return !children.isEmpty();
	}

	/**
	 * Adds a child id to the children array.
	 *
	 * @param cId the child id string to add
	 */
	public void addChild(String cId){
		if(!children.contains(cId)){
			children.add(cId);
		}
	}
	
	public ArrayList<String> getParents(){
		return parents;
	}
	
	public void setParents(ArrayList<String> parents){
		this.parents = parents;
	}
	
	public void addParent(String pId){
		if(!parents.contains(pId)){
			parents.add(pId);
		}
	}

	/**
	 * Compares one CompendiumNode to another, based on all the node values.
	 * @param aThat the other CompendiumNode to compare to
	 */
	@Override
	public boolean equals(Object aThat){
		if(this == aThat) return true;
		if(!(aThat instanceof CompendiumNode)) return false;
		CompendiumNode that = (CompendiumNode)aThat;
		return this.id.equalsIgnoreCase(that.getId())
				&& this.type==that.getType()
				&& this.author.equalsIgnoreCase(that.getAuthor())
				&& this.label.equalsIgnoreCase(that.getLabel())
				&& this.content.equalsIgnoreCase(that.getContent())
				&& this.children.equals(that.getChildren())
				&& this.parents.equals(that.getParents());
	}

	/**
	 * Calculates the hashcode for the CompendiumNode by XORing all values.
	 * @return the hashCode for the CompendiumNode
	 */
	@Override
	public int hashCode() {
		if (hashCode == 0) {
			hashCode = id.hashCode() ^ author.hashCode() ^ label.hashCode() ^ content.hashCode();
		}
		return hashCode;
	}

	/**
	 * Prints all the data contained in the node.
	 */
	public void printString(){
		System.out.println("============================================");
		String text = "Node ID\t : " + id + "\n";
		text += "Type\t : " + type + "\n";
		text += "Author\t : " + author + "\n";
		text += "Label\t : " + label + "\n";
		text += "Content\t : " + content + "\n";
		text += "Parents\t : " + parents + "\n";
		text += "Children : " + children + "\n";
		System.out.println(text);
		System.out.println("============================================");
	}
}
